<?php

include_once dirname(dirname(__FILE__)) . '/include/common.php';
$core = new core();
$customer_id = isset($_POST["customer_id"]) ? intval($_POST["customer_id"]) : 0;
$customer_name = isset($_POST["customer_name"]) ? addslashes(trim($_POST["customer_name"])) : '';
$customer_fb_name = isset($_POST["customer_fb_name"]) ? addslashes(trim($_POST["customer_fb_name"])) : '';
$customer_fb_link = isset($_POST["customer_fb_link"]) ? addslashes(trim($_POST["customer_fb_link"])) : '';
$customer_email = isset($_POST["customer_email"]) ? addslashes(trim($_POST["customer_email"])) : '';
$customer_phone = isset($_POST["customer_phone"]) ? addslashes(trim($_POST["customer_phone"])) : '';
$customer_extra_phone = isset($_POST["customer_extra_phone"]) ? addslashes(trim($_POST["customer_extra_phone"])) : '';
$customer_address = isset($_POST["customer_address"]) ? addslashes(trim($_POST["customer_address"])) : '';
$customer_size = isset($_POST["customer_size"]) ? addslashes(trim($_POST["customer_size"])) : '';
$customer_bank_account = isset($_POST["customer_bank_account"]) ? addslashes(trim($_POST["customer_bank_account"])) : '';
$customer_province = isset($_POST["customer_province"]) ? intval($_POST["customer_province"]) : 0;
$customer_wholesale = isset($_POST["customer_wholesale"]) ? intval($_POST["customer_wholesale"]) : 0;
$customer_vip = isset($_POST["customer_vip"]) ? intval($_POST["customer_vip"]) : 0;//0:bình thường,1: VIP, 2: blacklist
//if ($customer_name == "") {
//    echo json_encode(array('type' => 1, 'msg' => "Vui lòng điền tên khách hàng!"));
//    die;
//}
if ($customer_fb_name == "") {
    echo json_encode(array('type' => 1, 'msg' => "Vui lòng điền tên facebook!"));
    die;
}

$message = '';
if ($customer_id > 0) {
    if ($core->getCustomerById($customer_id)) {
        $core->db->query("UPDATE zen_customer SET name='$customer_name',facebook_name='$customer_fb_name',
            facebook_link='$customer_fb_link',phone='$customer_phone',extra_phone='$customer_extra_phone',
                address='$customer_address',clothes_size='$customer_size',bank_account='$customer_bank_account',
                    province=$customer_province,VIP=$customer_vip,email='$customer_email',wholesale=$customer_wholesale
             WHERE customer_id=$customer_id");
        $message = "Update thông tin khách hàng thành công!";
    } else {
        echo json_encode(array('type' => 1, 'msg' => "Khách hàng không tồn tại!"));
        die;
    }
} else {
    $core->db->query("INSERT INTO zen_customer (name,facebook_name,facebook_link
        ,phone,extra_phone,address,clothes_size,bank_account,province,VIP,email,wholesale)
                        VALUES('$customer_name','$customer_fb_name','$customer_fb_link',
                            '$customer_phone','$customer_extra_phone','$customer_address',
                                '$customer_size','$customer_bank_account',$customer_province,$customer_vip,'$customer_email',$customer_wholesale)");
    if ($core->db->insert_id > 0) {
        $message = "Thêm khách hàng thành công!";
    } else {
        echo json_encode(array('type' => 1, 'msg' => "Thêm khách hàng thất bại!"));
        die;
    }
}

echo json_encode(array('type' => 0, 'msg' => $message));
die;
?>